package com.cuit.qx.service.impl;

import com.cuit.qx.service.TQservice;
import com.cuit.qx.utils.FileUtils;
import com.cuit.qx.utils.HttpUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * @author ：long
 * @date ：Created in 2022/2/17 0017 11:31
 * @description：
 */
@Service
public class TQServiceImpl implements TQservice {

    @Value("${tqDataFileSavePath}")
    private String  tqFilePath;

    private String GRAPES_CFSV2Path="E:/data/ex_data/梧州/";

    String hour="3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,105,108,111,114,117,120,126,132,138,144,150,156,162,168,174,180,186,192,198,204,210,216,222,228,234,240";

    /**
     * GRAPES_CFSV2
     */
    @Override
    public void GRAPES_CFSV2(){
        String fileType="NAFP_FOR_FTM_GRAPES_CFSV2_NEHE/";
        String[] split = hour.split(",");
        for (String s : split) {
            HashMap<String,String> map=new HashMap<>();
            map.put("dataCode","NAFP_FOR_FTM_GRAPES_CFSV2_NEHE");
            map.put("interfaceId","getNafpEleGridInRectByTimeAndLevelAndValidtime");
            String date=getNowDate();
            map.put("time",date);
            map.put("fcstEle","TPE");
            map.put("levelType","1");
            map.put("fcstLevel","0");
            map.put("validTime",s);
            map.put("minLon","85");
            map.put("maxLon","125");
            map.put("minLat","5");
            map.put("maxLat","45");
            String dataByJSONToHttp = getDataByJSONToHttp(map);
            String fileName="NAFP_FOR_FTM_GRAPES_CFSV2_NEHE-TPE_1_0-85_125_5_45-"+ date + "_" + s + ".json";
            bufferToFile(dataByJSONToHttp,GRAPES_CFSV2Path+fileType,fileName);
        }
    }

    /**
     * GRAPES_3KM_SC
     */
    @Override
    public void GRAPES_3KM_SC(){
        String fileType="NAFP_FOR_GRAPES_3KM_SC/";
        String[] split = hour.split(",");
        for (String s : split) {
            HashMap<String,String> map=new HashMap<>();
            map.put("dataCode","NAFP_FOR_GRAPES_3KM_SC");
            map.put("interfaceId","getNafpEleGridByTimeAndLevelAndValidtime");
            String date=getNowDate();
            map.put("time",date);
            map.put("fcstEle","TPE");
            map.put("levelType","1");
            map.put("fcstLevel","0");
            map.put("validTime",s);
            String dataByJSONToHttp = getDataByJSONToHttp(map);
            String fileName="NAFP_FOR_GRAPES_3KM_SC-TPE_1_0-"+ date + "_" + s + ".json";
            bufferToFile(dataByJSONToHttp,GRAPES_CFSV2Path+fileType,fileName);
        }
    }

    /**
     * C1D_GLB_FOR
     */
    @Override
    public void C1D_GLB_FOR(){
        savaData(0,73,3);
        savaData(78,241,6);
    }

    public void savaData(int start,int end,int temp){
        String fileType="NAFP_ECMF_C1D_GLB_FOR/";
        for (int i=start;i<=end;i+=temp) {
            HashMap<String,String> map=new HashMap<>();
            map.put("dataCode","NAFP_ECMF_C1D_GLB_FOR");
            map.put("interfaceId","getNafpEleGridInRectByTimeAndLevelAndValidtime");
            String date=getNowDate();
            map.put("time",date);
            map.put("fcstEle","TPE");
            map.put("levelType","1");
            map.put("fcstLevel","0");
            map.put("validTime",String.valueOf(i));
            map.put("minLon","85");
            map.put("maxLon","125");
            map.put("minLat","5");
            map.put("maxLat","45");
            String dataByJSONToHttp = getDataByJSONToHttp(map);
            String fileName="NAFP_ECMF_C1D_GLB_FOR-TPE_1_0-85_125_5_45-"+ date + "_" + i + ".json";
            bufferToFile(dataByJSONToHttp,GRAPES_CFSV2Path+fileType,fileName);
        }
    }

    /**
     * SURF_CHN_MUL_HOR_N
     */
    @Override
    public void SURF_CHN_MUL_HOR_N(){
        String fileType="SURF_CHN_MUL_HOR_N/";
        HashMap<String,String> map=new HashMap<>();
        map.put("dataCode","SURF_CHN_MUL_HOR_N");
        map.put("interfaceId","getSurfEleInRectByTime");
        map.put("elements","Station_Name,Station_levl,Station_Id_C,Lat,Lon,Alti,Datetime,PRE_1h");
        String date=getNowDate();
        map.put("times",date);
        map.put("minLon","85");
        map.put("maxLon","125");
        map.put("minLat","5");
        map.put("maxLat","45");
        String dataByJSONToHttp = getDataByJSONToHttp(map);
        String fileName="SURF_CHN_MUL_HOR_N-85_125_5_45-"+ date +".json";
        bufferToFile(dataByJSONToHttp,GRAPES_CFSV2Path+fileType,fileName);
    }

    /**
     * SURF_CHN_MUL_HOR
     */
    @Override
    public void SURF_CHN_MUL_HOR(){
        String fileType="SURF_CHN_MUL_HOR/";
        HashMap<String,String> map=new HashMap<>();
        map.put("dataCode","SURF_CHN_MUL_HOR");
        map.put("interfaceId","getSurfEleInRectByTime");
        map.put("elements","Station_levl,Station_Id_C,Lat,Lon,Alti,Datetime,PRE_1h");
        String date=getNowDate();
        map.put("times",date);
        map.put("minLon","106");
        map.put("maxLon","116");
        map.put("minLat","18");
        map.put("maxLat","28");
        String dataByJSONToHttp = getDataByJSONToHttp(map);
        String fileName="SURF_CHN_MUL_HOR-106_116_18_28-"+ date+".json";
        bufferToFile(dataByJSONToHttp,GRAPES_CFSV2Path+fileType,fileName);

    }

    public static String getDataByJSONToHttp(HashMap<String, String> params) {
        long currentTimeMillis = System.currentTimeMillis();
        params.put("userId", "TEST");
        params.put("pwd", "TEST");
        params.put("dataFormat", "json");
        params.put("serviceNodeId", "NMIC_MUSIC_CMADAAS");
        HttpUtil httpUtil = new HttpUtil();
        String cimissData = "";
        try {
            cimissData = httpUtil.doPost("http://192.168.34.61:38889/music-ws/api?", params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        long currentTimeMillis2 = System.currentTimeMillis();
        System.out.println("cimiss访问耗时:" + (currentTimeMillis2 - currentTimeMillis));
        return cimissData;
    }

    public static String getNowDate(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(new Date().getTime());
    }

    public static void bufferToFile(String data,String path,String fileName) {
        FileUtils.checkFileAndCreateFile(path,fileName);
        FileWriter writer = null;
        try {
            writer = new FileWriter(path+fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            writer.write(data);
        } catch (IOException e1) {
            e1.printStackTrace();
        }finally {
            try {
                writer.flush();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
